package org.dairy.farms.nandana.core.logic;

import org.dairy.farms.dao.*;
import org.dairy.farms.model.*;
import org.dairy.farms.nandana.web.view.FeedView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jul 12, 2011
 * Time: 4:32:07 PM
 */
@Service(value = "feedManagementLogic")
public class FeedManagementLogicImpl implements FeedManagementLogic {
    private static final Logger logger = LoggerFactory.getLogger(FeedManagementLogicImpl.class);
    @Autowired
    private FeedTypeDAO feedTypeDao;
    @Autowired
    private FeedDAO feedDao;
    @Autowired
    private LivestockDAO livestockDao;
    @Autowired
    private ExpenseTypeDAO expenseTypeDao;
    @Autowired
    private ExpenseDAO expenseDao;
    @Autowired
    private FeedNotesDAO feedNotesDAO;

    public List<FeedType> getAllFeedTypes() {
        return feedTypeDao.loadAllObjects();
    }

    public List<Feed> feedDetails(Long livestockId, int page, int start, int limit) {
        return feedDao.getFeedDetails(livestockId, page, start, limit);
    }

    public long feedDetailsTotalCount(Long livestockId) {
        return feedDao.getFeedDetailsTotalCount(livestockId);
    }

    public Feed updateFeedDetails(FeedView feedView) {
        Feed feed = null;
        FeedType feedType = feedTypeDao.findByName(feedView.getFeedTypeName());
        //ExpenseType expenseType = expenseTypeDao.findByName(ExpenseType.LIVESTOCK_FEED_EXPENSE);
        Livestock livestock = livestockDao.findById(feedView.getLivestockId());
        if (feedView.getId() == null) {
            feed = new Feed();
            feed.setFeedingTime(feedView.getDateOfFeedeing());
            feed.setLivestock(livestock);
            feed.setFeedType(feedType);
            feed.setQuantity(feedView.getQuantity());
            feedDao.create(feed);

            /*FeedExpense feedExpense = createFeedExpense(feedView, expenseType);
            feedExpense.setFeed(feed);
            expenseDao.create(feedExpense);
*/
           /* feed.setFeedExpense(feedExpense);*/
            if(feedView.getNotes()!=null && !feedView.getNotes().isEmpty()){
                addFeedNotes(feed.getId(),feedView.getNotes());
            }
        } else {
            feed = feedDao.findById(feedView.getId());
            feed.setFeedingTime(feedView.getDateOfFeedeing());
            feed.setLivestock(livestock);
            feed.setFeedType(feedType);
            feed.setQuantity(feedView.getQuantity());
            
          /*  FeedExpense feedExpense = feed.getFeedExpense();
            feedExpense.setAmount(feedView.getCost());
            feedExpense.setPaymentDate(feedView.getDateOfPayment());
            feedExpense.setExpenseType(expenseType);
            feedExpense.setFeed(feed);
            expenseDao.update(feedExpense);

            feed.setFeedExpense(feedExpense);*/
            feedDao.update(feed);
        }
        return feed;

    }

    private FeedExpense createFeedExpense(FeedView feedView, ExpenseType expenseType) {
        FeedExpense feedExpense = new FeedExpense();
        feedExpense.setAmount(feedView.getCost());
        feedExpense.setExpenseType(expenseType);
        feedExpense.setPaymentDate(feedView.getDateOfPayment());
        return feedExpense;
    }

    @Override
    public Notes addFeedNotes(Long feedId, String notes) {
        FeedNotes feedNotes = new FeedNotes();
        feedNotes.setNote(notes);
        feedNotes.setFeed(feedDao.findById(feedId));
        return feedNotesDAO.create(feedNotes);
    }
}
